package com.pet.shop.utils;

import com.pet.shop.dto.PageDto;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Enumeration;

/**
 * Created by Administrator on 2016-1-4.
 */
public class PageCreator {

    public static <T extends Serializable> PageDto<T> createFromDataTables(HttpServletRequest request){
        PageDto<T> page = new PageDto<T>();
        String pageStart = request.getParameter("start");
        String pageSize = request.getParameter("length");
        if(pageSize != null && !"".equals(pageSize)){
            page.setRows(Integer.parseInt(pageSize));
        }
        if(pageStart != null && !"".equals(pageStart)){
            int pageNumber = Integer.parseInt(pageStart);
            pageNumber = (int)Math.ceil((double)pageNumber/page.getRows());
            page.setPage(pageNumber + 1);
        }
        //参数
        Enumeration<?> es = request.getParameterNames();
        while (es.hasMoreElements()) {
            String name = (String) es.nextElement();
            if(name.startsWith("search")) page.getParams().put(name.substring(7), request.getParameter(name));
            else if(name.startsWith("order")) page.getOrderBy().put(name.substring(6), request.getParameter(name));
        }
        return page;
    }

}
